# import pandas as pd
# from openpyxl import Workbook
# from openpyxl.styles import Font
#
# try:
#     # 创建DataFrame
#     df = pd.DataFrame(
#         data=[[1, 2, 3], [4, 5, 6]],
#         columns=['a', 'b', 'c'],
#         index=['x', 'y']
#     )
#
#     # 使用ExcelWriter，避免冗余的Workbook使用
#     with pd.ExcelWriter('test1.xlsx', engine='openpyxl') as writer:
#         df.to_excel(writer, sheet_name='Sheet1')
#
#         ws = writer.sheets['Sheet1']
#
#         # 设置索引列的单元格样式为非加粗
#         for cell in ws['A']:
#             cell.font = Font(bold=False)
#
# except Exception as e:
#     print(f"发生错误: {e}")



import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import Font

# 创建一个示例DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# 创建一个Excel写入器
writer = pd.ExcelWriter('output.xlsx', engine='openpyxl')

# 创建一个工作簿和工作表
workbook = writer.book
worksheet = workbook.create_sheet('Sheet1')

# 将DataFrame写入Excel文件，但不包括索引
df.to_excel(writer, index=True, sheet_name='Sheet1')

# 获取工作表
ws = writer.sheets['Sheet1']

# 设置列标题样式为非加粗
for col in ws.iter_cols(min_row=1, max_row=1, max_col=df.shape[1]):
    for cell in col:
        cell.font = Font(bold=False)

# # 设置行索引样式为非加粗
# # 由于之前已经通过index=False排除了行索引，这里不需要设置行索引样式
# # 如果需要设置行索引，请取消index=False，并设置以下样式
# for row in ws.iter_rows(min_col=1, max_col=1, max_row=df.shape[0] + 1):
#     for cell in row:
#         cell.font = Font(bold=False)

# 保存Excel文件
writer._save()
